home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / lkbackup.zip / ntmem.c < prev    next >
C/C++ Source or Header  |  1993-09-25  |  2KB  |  80 lines

  1. /* NOTE:  Do not call the next three routines directly.  Use the macros
  2.  * in handy.h, so that we can easily redefine everything to do tracking of
  3.  * allocated hunks back to the original New to track down any memory leaks.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <malloc.h>
  9. #include <process.h>
  10. #include "ntmem.h"
  11.  
  12. #define index   strchr
  13. #define FALSE 0
  14. #define TRUE 1
  15.  
  16. static void fatal( char * );
  17. static char nomem[] = "Out of memory!\n";
  18. static short nomemok = FALSE;
  19.  
  20.  
  21. char *safemalloc( size_t size)
  22. {
  23.     char *ptr;
  24.  
  25.     ptr = malloc(size?size:1);    /* malloc(0) is NASTY on our system */
  26.     if (ptr != Nullch)
  27.     return ptr;
  28.     else if (nomemok)
  29.     return Nullch;
  30.     else {
  31.     fputs(nomem,stderr) FLUSH;
  32.     exit(1);
  33.     }
  34.     /*NOTREACHED*/
  35. }
  36.  
  37. /* paranoid version of realloc */
  38.  
  39. char *saferealloc(char *where, size_t size)
  40. {
  41.     char *ptr;
  42.  
  43.     if (!where)
  44.     fatal("Null realloc");
  45.     ptr = realloc(where,size?size:1);    /* realloc(0) is NASTY on our system */
  46.     if (ptr != Nullch)
  47.     return ptr;
  48.     else if (nomemok)
  49.     return Nullch;
  50.     else {
  51.     fputs(nomem,stderr) FLUSH;
  52.     exit(1);
  53.     }
  54.     /*NOTREACHED*/
  55. }
  56.  
  57. /* safe version of free */
  58.  
  59. void safefree(char *where)
  60. {
  61.     if (where) {
  62.     free(where);
  63.     }
  64. }
  65.  
  66.  
  67. void *memzero( char *pcszTarget, size_t lNumBytes )
  68. {
  69.     return( memset( pcszTarget, 0, lNumBytes ) );
  70. }
  71.  
  72. /* Print an error message containing the string TEXT, then exit.  */
  73.  
  74. static void fatal(char *string)
  75. {
  76.     fprintf(stderr, "%s\n", string);
  77.     exit(2);
  78. }
  79.  
  80.